//
//  SearchViewController.m
//  jinnengExam
//
//  Created by 陈洪锋 on 2023/10/30.
//  Copyright © 2023 jznrj. All rights reserved.
//

#import "SearchViewController.h"
#import "NewsDetailViewController.h"
#import "VideoDetailViewController.h"
#import "TopicVideoViewController.h"

@interface SearchViewController ()<UISearchBarDelegate>
@property(nonatomic,strong) JznWebView *jznWebView;
@property(nonatomic) int page;
@property(nonatomic,strong) NSString *module;
@property(nonatomic,strong) NSString * trade;
@property(nonatomic) CGRect frame;
@property(nonatomic,strong) NSString *searchText;
@property(nonatomic,strong) UISearchBar *searchBar;
@end

@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"搜索";
    UINavigationBar * bar = self.navigationController.navigationBar;
    UIImage *bgImage = [ColorUtil navigationBarColor:[ColorUtil titleColor] BarSize:bar.frame alphe:1.0];
    [bar setBackgroundImage:bgImage forBarMetrics:UIBarMetricsDefault];
    [bar setTintColor:[UIColor whiteColor]];
    [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]}forState:UIControlStateNormal];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,nil]];
    [self initView];
     self.tabBarController.tabBar.frame = CGRectMake(0, 0, 0, 0);
     self.tabBarController.tabBar.hidden = YES;
    [self registerNativeFunctions];
}

-(void) viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = NO;
}

-(void) initView{
    self.searchBar = [[UISearchBar alloc]init];
    self.searchBar.searchBarStyle = UISearchBarStyleDefault;
    self.searchBar.placeholder = @"请输入搜索内容";
    self.searchBar.delegate = self;
    //self.searchBar.showsBookmarkButton = YES;
    [self.view addSubview:self.searchBar];
    [self.searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
         make.left.right.equalTo(self.view);
        
         if (@available(iOS 11.0, *)) {
             make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
         } else {
             make.top.equalTo(self.view);
         }
     }];
    
   [self.view addSubview:self.jznWebView];
    self.jznWebView.scrollView.bounces = YES;
   [self.jznWebView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.mas_equalTo(self.searchBar.mas_bottom);

        if (@available(iOS 11.0, *)) {
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
        } else {
            make.bottom.equalTo(self.view);
        }
    }];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.jznWebView loadLocalFile:@"/ios_jinneng/jinneng_search" type:@"html"];

    self.jznWebView.scrollView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        [self requestLearn:++self.page];
    }];
    
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    self.searchText =searchBar.text;
    if(self.searchText!=nil && ![self.searchText isEqualToString:@""]){
        [self requestLearn:1];
    }
}
- (void)registerNativeFunctions {
    
    [self.webViewBridge registerHandler:@"toNewsDetailViewController" handler:^(id data, WVJBResponseCallback responseCallback) {

        dispatch_async(dispatch_get_main_queue(), ^(){
            NewsDetailViewController *newsDetailViewController = [[NewsDetailViewController alloc]init];
            [newsDetailViewController setParams:[data mj_JSONString]];
            self.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:newsDetailViewController animated:YES];
        });
    }];
    
    [self.webViewBridge registerHandler:@"toVideoDetailViewController" handler:^(id data, WVJBResponseCallback responseCallback) {
                    
            NSString *dataId =data[@"dataId"];
            NSString *cover = data[@"coverUrl"];
            dispatch_async(dispatch_get_main_queue(), ^(){
                VideoDetailViewController *vc = [[VideoDetailViewController alloc]init];
                vc.videoNewsId = dataId;
                vc.imageUrl = cover;
                [self.navigationController pushViewController:vc animated:YES];
            });
    }];
    
    [self.webViewBridge registerHandler:@"toTopicDetailViewController" handler:^(id data, WVJBResponseCallback responseCallback) {
           NSString *dataId = data[@"dataId"];

            dispatch_async(dispatch_get_main_queue(), ^(){
                TopicVideoViewController *vc = [[TopicVideoViewController alloc]init];
                vc.topicId = dataId;
                [self.navigationController pushViewController:vc animated:YES];
            });
    }];
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
}

-(void) requestLearn:(int) page{
    self.page = page;
    NSString *url = [NSString stringWithFormat:@"%@/news/queryNewsList" ,[URLUtil hostName]];
    NSDictionary *params = @{@"newsTitle":self.searchText,@"pageNum":[NSString stringWithFormat:@"%d",page],@"pageSize":@"10"};
    
    [JZNRequestUtil requestUrl:url requestParams:params success:^(DataResult *dataResult) {
        if(dataResult.code == 0){
            if (page == 1) {
                NSString *clear = @"clear()";
                [self.jznWebView evaluateJavaScript:clear completionHandler:^(id _Nullable data, NSError * _Nullable error) {
                    
                }];
                [self.jznWebView.scrollView.mj_header endRefreshing];
                [self.jznWebView.scrollView.mj_footer resetNoMoreData];
            }
            
            NSString *clear = [NSString stringWithFormat:@"getNewsList(%@)",[dataResult.data mj_JSONString]];
            [self.jznWebView evaluateJavaScript:clear completionHandler:^(id _Nullable data, NSError * _Nullable error) {
                
            }];
            NSArray *array = dataResult.data;
            if([array count]<10){
                
                [self.jznWebView.scrollView.mj_footer endRefreshingWithNoMoreData];
            }else{
                
                [self.jznWebView.scrollView.mj_footer endRefreshing];
            }
        }else {
        }
    } failure:^(NSError *error) {
        
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
